package com.lb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lb.common.Result;
import com.lb.entity.DTO.LoginDTO;
import com.lb.entity.DTO.RegisterDTO;
import com.lb.entity.DTO.StudentDTO;
import com.lb.entity.Query;
import com.lb.entity.Student;
import com.lb.entity.VO.PageResultVO;
import com.lb.service.StudentService;
import com.lb.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping
@Slf4j
public class UserController {
    @Autowired
    private StudentService studentService;
    @PostMapping("/user/all")
    public Result<PageResultVO> getAll(@RequestBody Query query) {
        Page<Student> bypage = studentService.getBypage(query);
        PageResultVO pageResultVO = new PageResultVO();
        pageResultVO.setRecords(bypage.getRecords());
        pageResultVO.setTotal((int) bypage.getTotal());
        // List<Student> list = studentService.list();
       // System.out.println(list);
        //System.out.println("sds");
         return Result.success(pageResultVO);
    }
    @Transactional
    @GetMapping("/user/get")
    public  Result<List<Student>> getByName(@RequestParam String name) {
            List<Student> list =studentService.getByname(name);
            return Result.success(list);
        }
    @PostMapping("/user/add")
    @Transactional
    public Result add(@RequestBody StudentDTO student) {
      //  student.setId(null);
        Student student1 = new Student();
        BeanUtils.copyProperties(student,student1);
        studentService.save(student1);
        return Result.success();
    }
    @RequestMapping("/user/login")
    public Result login(@RequestBody LoginDTO loginDTO)
    {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginDTO.getUsername());
        queryWrapper.eq("password",loginDTO.getPassword());
         Student student = studentService.getOne(queryWrapper);
      if (student == null)
      {
          return Result.error("失败");
      }
        Map<String,Object> map = new HashMap<>();
        map.put("id", student.getId());
        // map.put("name",user.getName());
        map.put("username", student.getUsername());
        String s = JwtUtils.generateJwt(map);
       String token= JwtUtils.generateJwt(map);
       System.out.println(token);
      return Result.success(token);
     }
     @GetMapping("/user/name")
     public Result getName(@RequestParam String name) {
         long username = studentService.count(new QueryWrapper<Student>().eq("username", name));
         if (username == 0) {
             return Result.error("sdd");
         } else if (username != 0) {
             return Result.success();
         }
         return null;
     }
   @Transactional
      @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        Student student = new Student();
        student.setUsername(registerDTO.getUsername());
        student.setPassword(registerDTO.getPassword());
        studentService.save(student);
        return Result.success();
      }
      @GetMapping("/student/byusername")
    public Result getByUsername(@RequestParam String username) {
        Student student =new Student();
        student.setUsername(username);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
          Student one = studentService.getOne(queryWrapper);
          return Result.success(one);
    }
}